﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;

namespace ProjectSem40701.Admin
{
    public partial class QuestionAdd : System.Web.UI.Page
    {
        /// <summary>
        /// Declare member variales here
        /// </summary>
        private const string SUCCESS = "success";
        private const string FAIL = "fail";

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        /// <summary>
        /// Thêm câu hỏi
        /// </summary>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                string strUser = System.Web.HttpContext.Current.User.Identity.Name;
                string strUserID = Membership.GetUser(strUser).ProviderUserKey.ToString();
                Guid userID = new Guid(strUserID);

                //System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");
                //DateTime dtCreatedDate = Convert.ToDateTime(this.txtQuestionCreatedDate.Text.Trim());

                AdminXSD.QuestionTableAdapters.tbl_QuestionTableAdapter dp =
                        new AdminXSD.QuestionTableAdapters.tbl_QuestionTableAdapter();
                int ok = dp.tbl_Question_Insert(userID,
                    Convert.ToInt32(this.cbbSubjectID.SelectedValue.Trim()),
                    this.txtTitle.Text.Trim(),
                    Server.HtmlEncode(this.txtQuestionContent.Value.Trim()),
                    DateTime.Now,
                    this.txtQuestionDescription.Text.Trim(),
                    Convert.ToInt32(this.cbbQuestionLevelID.SelectedValue.Trim()),
                    Convert.ToInt32(this.txtQuestionMark.Text.Trim()),
                    Convert.ToInt32(this.cbbStatus.SelectedValue.Trim()));

                if (ok > 0)
                {
                    //bao thanh cong
                    Show_Dialog(SUCCESS);
                }
                else
                {
                    //bao that bai
                    Show_Dialog(FAIL);
                }
            }
            catch
            { 
                //bao that bai
                Show_Dialog(FAIL);
            }
        }
        /// <summary>
        /// Close diaglog here
        /// </summary>
        private void Close_Dialog()
        {
            switch (this.lblSuccess.Text)
            {
                case SUCCESS:
                    Response.Redirect("Question.aspx");
                    break;
                case FAIL:
                    this.btnAdd_ModalPopupExtender.Enabled = false;
                    break;
                default:
                    this.btnAdd_ModalPopupExtender.Enabled = false;
                    break;
            }
        }

        /// <summary>
        /// Show dialog here
        /// </summary>
        private void Show_Dialog(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                    this.ltrMessage.Text = "Thành công";
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Thất bại";
                    break;
                default:
                    this.ltrMessage.Text = "Không thành công";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnAdd_ModalPopupExtender.Enabled = true;
            this.btnAdd_ModalPopupExtender.Show();
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

        protected void btnYes_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }
    }
}
